const router = require("@koa/router")();
// const upload = require("../../plugin/upload")("/upload/image");
// const Goods = require('../../model/admin/Goods');
// const Category = require("../../model/admin/Category");
const { localTime } = require("../../utils/index");
const { host } = require("../../config/index");
router.post("/add", async (ctx) => {
  const name = ctx.request.body.name || ctx.request.body.title;
  if (name) {
    try {
      const findRes = await ctx.mongoose.find({ "name": name });
      const modelName = ctx.mongoose.modelName;
      if (findRes.length > 0) {
        let message = "该分类已经存在";
        modelName === "Category" ? message = "该分类已经存在" : modelName === "Goods" ? message = "该物品已经存在" : message = "该英雄已经存在";
        ctx.body = {
          status: 1,
          message: message,
          data: null
        }
      } else {
        const res = await ctx.mongoose.create({ ...ctx.request.body, time: localTime() });
        if (res.__v === 0) {
          ctx.body = {
            status: 1,
            message: "创建成功",
            data: res
          }
        } else {
          ctx.body = {
            status: 0,
            message: "创建失败",
            data: null
          }
        }
      }

    } catch (error) {
      console.log(error)
      ctx.body = {
        status: 0,
        message: "创建失败",
        data: error
      }
    }
  } else {
    ctx.body = {
      status: 0,
      message: "请传递需要创建名称",
      data: null
    }
  }
});
router.get("/inquire", async (ctx) => {
  let res;
  try {
    
    /* 查询分类 */
    if (ctx.mongoose.modelName === "Category") {
      res = await ctx.mongoose.find().populate({ path: 'parent', model: Category }); //.limit(10)
      /* y英雄 */
    } else if (ctx.mongoose.modelName === "Hero") {
      res = await ctx.mongoose.find().populate({ path: 'goods', model: Goods }).populate({ path: 'categories', model: Category }).populate({ path: 'partners' }).limit(10);
      /* 其他不需要关联 */
    } else if (ctx.mongoose.modelName === "Step") {
      res = await ctx.mongoose.aggregate([{ $lookup: { from: 'users', localField: 'openId', foreignField: 'openId', as: 'users' } }]).limit(10)
    } else {
      res = await ctx.mongoose.find().limit(10);
    }
    // const res = await ctx.mongoose.find().setOptions(queryOptions);
    ctx.body = {
      status: 200,
      message: "成功",
      data: res
    }
  } catch (error) {
    console.log(error)
    ctx.body = {
      status: 0,
      message: "查询失败",
      data: error
    }
  }
})
router.get("/inquire/:aid", async (ctx) => {

  if (ctx.params.aid) {
    try {
      const res = await ctx.mongoose.findById(ctx.params.aid);
      ctx.body = {
        status: 1,
        message: "成功",
        data: res
      }
    } catch (error) {
      ctx.body = {
        status: 0,
        message: "查询失败",
        data: null
      }
    }
  } else {
    ctx.body = {
      status: 0,
      message: "缺少必要参数",
      data: null
    }

  }
})

router.post("/update", async (ctx) => {

  if (ctx.request.body.id) {
    try {
      const res = await ctx.mongoose.findByIdAndUpdate(ctx.request.body.id, ctx.request.body.query, { new: true });
      if (res.__v === 0) {
        ctx.body = {
          status: 1,
          message: "更新成功",
          data: res
        }
      } else {
        ctx.body = {
          status: 0,
          message: "更新失败",
          data: res
        }
      }

    } catch (error) {
      ctx.body = {
        status: 0,
        message: "更新失败",
        data: error
      }
    }
  } else {
    ctx.body = {
      status: 0,
      message: "缺少必要参数",
      data: null
    }

  }
})
router.post("/delete", async (ctx) => {
  if (ctx.request.body.id) {
    try {
      const res = await ctx.mongoose.findByIdAndDelete(ctx.request.body.id);

      if (res.__v === 0) {
        ctx.body = {
          status: 1,
          message: "删除成功",
          data: res
        }
      } else {
        ctx.body = {
          status: 0,
          message: "删除失败",
          data: res
        }
      }

    } catch (error) {
      ctx.body = {
        status: 0,
        message: "更新失败",
        data: null
      }
    }
  } else {
    ctx.body = {
      status: 0,
      message: "缺少必要参数",
      data: null
    }

  }
})
/* 图片上传功能 */
// router.post('/upload', upload.single('file'), ctx => {
//   if (ctx.file) {

//     ctx.body = {
//       status: 1,
//       message: "上传成功",
//       data: host + "/upload/image/" + ctx.file.filename
//     };
//   } else {
//     ctx.body = {
//       status: 10,
//       message: "上传失败",
//       data: null
//     };

//   }
// });
router.get("/", async (ctx) => {
  ctx.body = {
    message: "成功",
    status: 1,
    data: "欢迎来到目录模块"
  }
});
module.exports = router;